package com.redrose.videodemo.activity;

import android.Manifest;
import android.media.AudioRecord;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.redrose.videodemo.R;
import com.redrose.videodemo.base.BaseActivity;
import com.redrose.videodemo.runnabletask.AudioRecordRunnable;
import com.redrose.videodemo.runnabletask.PlayPCMRunnable;
import com.redrose.videodemo.utils.AudioRecordUtil;
import com.redrose.videodemo.utils.IntentUtils;
import com.redrose.videodemo.utils.LogUtils;
import com.redrose.videodemo.utils.PathUtil;
import com.redrose.videodemo.utils.PcmToWavUtil;
import com.redrose.videodemo.utils.ThreadPoolUtil;
import com.redrose.videodemo.utils.ThreadTask;
import com.redrose.videodemo.utils.ToastUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.File;

/**
 * Desc: 音视频学习
 *
 * @author: RedRose
 * Date: 2019/4/15 0015
 * Email: yinsxi@163.com
 */
@ContentView(R.layout.activity_learn_plan)
public class LearnPlanActivity extends BaseActivity {
    @ViewInject(R.id.record)
    private TextView mRecordView;
    /**
     * 声明recoordBufffer的大小字段
     */
    private int mRecordBufSize = 0;
    private AudioRecord mAudioRecord;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] permissoons = new String[]{
                Manifest.permission.RECORD_AUDIO
        };
        if (!hasPermission(permissoons[0])) {
            requestPeermission(permissoons, 0);
        }
    }

    @Event(value = {R.id.record,
            R.id.stopRecord,
            R.id.playWavRecord,
            R.id.camera_textureView,
            R.id.camera_glsurfaceview,
            R.id.camera_surfaceview,
            R.id.playRecord})
    private void onClickView(View view) {
        int id = view.getId();
        switch (id) {
            //开始录音 pcm
            case R.id.record:
                initAudioRecord();
                startRecording();
                break;
            //停止录音
            case R.id.stopRecord:
                stopRecord();
                break;
            //播放pcm
            case R.id.playRecord:
                ToastUtil.show(mContext.getApplicationContext(), "开始播放PCM录音");
                ThreadTask.getInstance().executorOtherThread(new PlayPCMRunnable(mContext), ThreadTask.ThreadPeriod.PERIOD_HIGHT);
                break;
            //PCM to WAV
            case R.id.playWavRecord:
                ToastUtil.show(mContext, "开始转换格式PCM to wav");
                plawWavRecord();
                break;
            case R.id.camera_textureView:
                IntentUtils.goToCameraTextureActivity(mContext, null);
                break;
            case R.id.camera_surfaceview:
                IntentUtils.goToCameraSurfaceViewActivity(mContext, null);
                break;
            case R.id.camera_glsurfaceview:
                IntentUtils.goToCameraGLSurfaceViewActivity(mContext, null);
                break;

            default:
                break;
        }
    }

    /**
     * pcm to wav
     */
    private void plawWavRecord() {
        PcmToWavUtil pcmToWavUtil = new PcmToWavUtil(AudioRecordUtil.AUDIO_SAMPLE_RATE, AudioRecordUtil.CHANNEL_CONFIG, AudioRecordUtil.AUDIO_FORMAT);
        String inFilename = PathUtil.getAppAudioPath() + "audio.pcm";
        File inFile = new File(inFilename);
        if (!inFile.exists()) {
            ToastUtil.show(mContext, "原始音频数据文件不存在");
            return;
        }
        pcmToWavUtil.pcmToWav(PathUtil.getAppAudioPath() + "audio.pcm", PathUtil.getAppAudioPath() + "audioWav.wav");
        ToastUtil.show(mContext.getApplicationContext(), "转换格式结束");
        LogUtils.d(TAG, "--转换格式结束--");
    }

    /**
     * 初始化AudioRecord
     */
    private void initAudioRecord() {
        mRecordBufSize = AudioRecord.getMinBufferSize(AudioRecordUtil.AUDIO_SAMPLE_RATE, AudioRecordUtil.CHANNEL_CONFIG, AudioRecordUtil.AUDIO_FORMAT);
        mAudioRecord = AudioRecordUtil.getInstance().getAudioRecord(mRecordBufSize);
    }

    /**
     * 开始录音
     */
    private void startRecording() {
        ToastUtil.show(mContext.getApplicationContext(), "开始录音");
        LogUtils.d(TAG, "--开始录音--");
        ThreadTask.getInstance().executorOtherThread(new AudioRecordRunnable(mContext), ThreadTask.ThreadPeriod.PERIOD_HIGHT);
    }

    /**
     * 停止录音
     */
    private void stopRecord() {
        AudioRecordUtil.getInstance().setRecordStatues(false);
    }
}
